package com.sphd.emanu.activity.arrivetime;

import com.google.gson.JsonObject;
import com.sphd.emanu.mvp.BasePresenterImpl;
import com.sphd.emanu.pojo.market.ArriveTime;
import com.sphd.emanu.utils.GsonUtil;
import com.wzutils.L;

import java.util.List;

import cn.pedant.SweetAlert.SweetAlertDialog;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * MVPPlugin
 *  邮箱 784787081@qq.com
 */

public class ArriveTimePresenter extends BasePresenterImpl<ArriveTimeContract.View> implements ArriveTimeContract.Presenter{

    @Override
    public void selectArriveTime(int org) {
        emanuService.selectArriveTime(org).enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                L.e(response.toString());
                List<ArriveTime> timeList = GsonUtil.jsonToList(response.body(),ArriveTime.class);
                mView.setTimeList(timeList);
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
            Error(t);
            }
        });
    }

    @Override
    public void addArriveTime(int org, String arriveTime, Integer offset) {
            emanuService.addArriveTime(org, arriveTime, offset).enqueue(new Callback<JsonObject>() {
                @Override
                public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
                    L.e(response.toString());
                    mView.setRes(res(response));
                }

                @Override
                public void onFailure(Call<JsonObject> call, Throwable t) {
                    Error(t);
                }
            });
    }

    @Override
    public void updateArriveTime(Integer id,String arriveTime, Integer offset, final Integer pos) {
        emanuService.updateArriveTime(id,arriveTime, offset).enqueue(new Callback<JsonObject>() {
            @Override
            public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
                L.e(response.toString());
                mView.upRes(res(response),pos);
            }

            @Override
            public void onFailure(Call<JsonObject> call, Throwable t) {
            Error(t);
            }
        });
    }

    @Override
    public void deleteArriveTime(Integer arriveTimeId, final int pos, final SweetAlertDialog sweetAlertDialog) {
            emanuService.deleteArriveTime(arriveTimeId).enqueue(new Callback<JsonObject>() {
                @Override
                public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
                    L.e(response.toString());
                    mView.delRes(res(response),pos,sweetAlertDialog);
                }

                @Override
                public void onFailure(Call<JsonObject> call, Throwable t) {
                    Error(t);
                }
            });
    }


}
